/***copyright (c) 2016 ZhangGuoLi (imeilige@gmail.com) ***/
package cn.zgl.gfw;

import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;


/**
 * @author imeilige@gmail.com
 */
public class Iss {
	
	JSONArray configs = JSONArray.fromObject("");
	HashMap<String, String> data = new HashMap<String, String>();
	//判断网页是否更新
	public String getLastlastModified(String url) throws Exception{
		URL u = new URL(url);
		URLConnection connection = u.openConnection();
		Long timestamp = connection.getLastModified();
		return new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date(timestamp));
	}
	//获取数据
	public void getAccount(String url) throws Exception{
		Document doc = Jsoup.connect(url).get();
		Element free = doc.getElementById("free");
		Elements account = free.getElementsByClass("col-lg-4");
		String server = "";
		String server_port = "";
		String password= "";
		String method = "method";
		String remarks = "";
		String auth = "false";
		for(int i=0;i<account.size();i++){
			Document pro = Jsoup.parse(account.get(i)+"");
			server = pro.select("h4").get(0).toString().substring(11, 21);
			server_port = pro.select("h4").get(1).toString().split(":")[1].split("<")[0];
			password = pro.select("h4").get(2).toString().split(":")[1].split("<")[0];
			method = pro.select("h4").get(3).toString().substring(9, 20);
			remarks = pro.select("h4").get(0).toString().substring(4, 8);
			dataToFile(server, server_port, password, method, remarks, auth);
		}
	}
	//写入内容
	public boolean dataToFile(String server,String server_port,String password,String method,String remarks,String auth) throws Exception{
		Map<String, String> param = new HashMap<String, String>();
		param.put("server", server_port);
		param.put("server_port", server_port);
		param.put("password", password);
		param.put("method", method);
		param.put("remarks", remarks);
		param.put("auth", auth);
		JSONObject config = JSONObject.fromObject(param);//每一个数据
		
		return true;
	}
}
